/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2021
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
import fwlog from '@/core/fwlog.js';
import fwmd5 from '@/core/fwmd5.js';

const fwsign = {
  makeSign: function (obj,keysecret) {
    if (!obj) {
      fwlog.error('需要加密的数组对象为空');
    }
    var str = '';
    var secret = keysecret;
    if (!secret) {
      fwlog.error('密钥未获取');
    }
    //生成key升序数组
    var arr = Object.keys(obj);
    arr.sort();
    for (var i in arr) {
      str += (arr[i] + '='+obj[arr[i]]+'&');
    }
    let plaintext = str + 'key=' + secret;
    fwlog.debug(plaintext);
    var encrypted = fwmd5.hexMD5(plaintext);
    return encrypted;
  },
  createRndStr: function (len){
    let forNum = len/32+1;
    let tmpStr = '';
    for(let i=0; i<forNum; i++){
      tmpStr += fwmd5.hexMD5(this.createTimeStamp());
    }
    return tmpStr.substr(0,len);
  },
  createRndNum:function(len){
    let tmpRndStr = Math.random().toString();
    let forNum = len / (tmpRndStr.length - 2) + 1;
    let tmpStr = '';
    for (let i = 0; i < forNum; i++) {
      tmpStr += Math.random().toString().substr(2);
    }
    return tmpStr.substr(0,len);
  },
  createTimeStamp: function (len){
    return parseInt(new Date().getTime() / 1000) + '';
  }
}

export default fwsign;
